package com.zhaoxi.project.wms.service.impl;

import java.util.List;

import com.zhaoxi.common.utils.DateUtils;
import com.zhaoxi.framework.web.server.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

import com.zhaoxi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.zhaoxi.project.wms.domain.WmsOrderLine;
import com.zhaoxi.project.wms.mapper.WmsOrderMapper;
import com.zhaoxi.project.wms.domain.WmsOrder;

/**
 * 跟物品有关的订单Service业务层处理
 *
 * @author zhaoxi
 * @date 2023-07-22
 */
@Service
public class WmsOrderServiceImpl extends BaseServiceImpl {
    @Autowired
    private WmsOrderMapper wmsOrderMapper;

    /**
     * 查询跟物品有关的订单
     *
     * @param id 跟物品有关的订单主键
     * @return 跟物品有关的订单
     */

    public WmsOrder selectWmsOrderById(Long id) {
        WmsOrder wmsOrder = wmsOrderMapper.selectWmsOrderById(id);
        List<WmsOrder> list = wmsOrderMapper.selectWmsOrderLineList(wmsOrder);
        wmsOrder.setLineList(list);
        return wmsOrder;
    }

    /**
     * 查询跟物品有关的订单列表
     *
     * @param wmsOrder 跟物品有关的订单
     * @return 跟物品有关的订单
     */

    public List<WmsOrder> selectWmsOrderList(WmsOrder wmsOrder) {
        return wmsOrderMapper.selectWmsOrderList(wmsOrder);
    }

    /**
     * 新增跟物品有关的订单
     *
     * @param wmsOrder 跟物品有关的订单
     * @return 结果
     */
    @Transactional
    public String insertWmsOrder(WmsOrder wmsOrder) {
        wmsOrder.setCreateTime(DateUtils.getNowDate());
        wmsOrder.setCreateBy(this.getUsername());
        wmsOrder.setUpdateTime(DateUtils.getNowDate());
        wmsOrder.setUpdateBy(this.getUsername());
        wmsOrder.setTenantId(this.getTenantId());
        wmsOrderMapper.insertWmsOrder(wmsOrder);
        if (wmsOrder.getId() == null){
            return "保存订单失败";
        }
        List<WmsOrder> list = wmsOrder.getLineList();
        for(int i = 0;i<list.size();i++){
            WmsOrder wmsOrderLine = list.get(i);
            wmsOrderLine.setId(wmsOrder.getId());
            this.insertWmsOrderLine(wmsOrderLine);
        }
        return null;
    }

    /**
     * 修改跟物品有关的订单
     *
     * @param wmsOrder 跟物品有关的订单
     * @return 结果
     */
    @Transactional
    public int updateWmsOrder(WmsOrder wmsOrder) {
        wmsOrder.setUpdateTime(DateUtils.getNowDate());
        wmsOrder.setUpdateBy(this.getUsername());
        int row = wmsOrderMapper.updateWmsOrder(wmsOrder);
        List<WmsOrder> list = wmsOrder.getLineList();
        for (int i=0;i < list.size(); i++){
            WmsOrder wmsOrderLine = list.get(i);
            
            if (wmsOrderLine.getLineId() == null){
                wmsOrderLine.setId(wmsOrder.getId());
                this.insertWmsOrderLine(wmsOrderLine);
            }else{
                this.updateWmsOrderLine(wmsOrderLine);
            }
        }
        return row;
    }

    /**
     * 批量删除跟物品有关的订单
     *
     * @param ids 需要删除的跟物品有关的订单主键
     * @return 结果
     */
    @Transactional
    public int deleteWmsOrderByIds(Long[] ids) {
//        wmsOrderMapper.deleteWmsOrderLineByIds(ids);
        return wmsOrderMapper.deleteWmsOrderByIds(ids);
    }

    public int insertWmsOrderLine(WmsOrder wmsOrder){
        wmsOrder.setCreateBy(this.getUsername());
        wmsOrder.setUpdateTime(DateUtils.getNowDate());
        wmsOrder.setUpdateBy(this.getUsername());
        wmsOrder.setTenantId(this.getTenantId());
        return wmsOrderMapper.insertWmsOrderLine(wmsOrder);
    }

    public int updateWmsOrderLine(WmsOrder wmsOrder) {
        wmsOrder.setUpdateTime(DateUtils.getNowDate());
        wmsOrder.setUpdateBy(this.getUsername());
        return wmsOrderMapper.updateWmsOrderLine(wmsOrder);
    }
}
